//
//  MainUIStyleMng.m
//  habir
//
//  Created by huck on 15-9-2.
//  Copyright (c) 2015年 custom. All rights reserved.
//

#import "MainUIStyleMng.h"
#import "AppColor.h"
#import "AppUtils.h"
#import "UIImageView+WebCache.h"
#import "MainViewController.h"
#import "SuperviseViewController.h"

#define STYLE_SUFFIX    @"_style"


@implementation MainUIStyleMng
@synthesize m_iStyleIndex;


#pragma mark --private function
- (UITabBarItem *)setTabBarItem:(UITabBarItem *)barItem image:(NSString *)image imageSel:(NSString *)imageSel normalAttrs:(NSDictionary *)normalAttrs selectedAttrs:(NSDictionary *)selectedAttrs
{
    [barItem setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [barItem setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];

    UIImage *img = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *imgSel = [[UIImage imageNamed:imageSel] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [barItem setImage:img];
    [barItem setSelectedImage:imgSel];

    return barItem;
}

#pragma mark --public function

+ (MainUIStyleMng *) sharedInstance
{
    static MainUIStyleMng * s_pMainUIStyleMng = nil;
    if(s_pMainUIStyleMng)
    {
        return s_pMainUIStyleMng;
    }
    
    s_pMainUIStyleMng = [[MainUIStyleMng alloc] initWithStyleIndex:0];
    
    return s_pMainUIStyleMng;
}

+ (BOOL)canSupportStyle:(int) iStyleIndex
{
    return YES;
}

- (id)initWithStyleIndex:(int)iStyleIndex
{
    self = [super init];
    if (self)
    {
        m_iStyleIndex = iStyleIndex;
    }
    return self;
}

- (void)setTabBarStyle:(UITabBar *)tabBar
{
    // 用颜色设置
    UIColor *bgcolor = [UIColor colorWithString:COLOR_FFFFFF];
    tabBar.backgroundImage = [UIColor imageWithColor:bgcolor];
}

- (void)setTabBarItemStyle:(UITabBarItem *)pTabBarItem
{
    UIFont *font = [UIFont systemFontOfSize:12];
    
    UIColor *tintColor = nil;
    NSDictionary *normalAttrs = nil;
    NSDictionary *selectedAttrs = nil;

    tintColor = [UIColor colorWithString:COLOR_17A4FF];
    normalAttrs = [NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithString:COLOR_DEDEDE], NSForegroundColorAttributeName, font, NSFontAttributeName, nil];
    selectedAttrs = [NSDictionary dictionaryWithObjectsAndKeys:tintColor, NSForegroundColorAttributeName, font, NSFontAttributeName, nil];
    
    //0:my,1:group,2:supervise,3:review
    NSArray * pImgFileArray = [NSArray arrayWithObjects:@"tab_home_nor",@"tab_order_nor",@"tab_message_nor",@"tab_me_nor", nil];
    
    NSArray * pImgFileSelArray = [NSArray arrayWithObjects:@"tab_home_pre",@"tab_order_pre",@"tab_message_pre",@"tab_me_pre", nil];
    
    NSString * strImgNor = [pImgFileArray objectAtIndex:pTabBarItem.tag];
    NSString * strImgSel = [pImgFileSelArray objectAtIndex:pTabBarItem.tag];
    
    [self setTabBarItem:pTabBarItem image:strImgNor imageSel:strImgSel normalAttrs:normalAttrs selectedAttrs:selectedAttrs];
    
    //对图片进行拉伸偏移
    pTabBarItem.imageInsets = UIEdgeInsetsMake(-2, 0, 2, 0);
}

- (void)setNavigationItemTitleStyle:(UINavigationItem *)pItem titleIndex:(NSInteger)iTitleIndex title:(NSString *)strTitle
{
    if(iTitleIndex == TabBarItemTypeTask || iTitleIndex == TabBarItemTypeHome|| iTitleIndex == TabBarItemTypeProject)
    {
        pItem.titleView = nil;
        pItem.title = strTitle;
    }
    else
    {
        int iItemHeight = 44;
        CGRect frame = CGRectMake(0, 0, SCREEN_WIDTH, iItemHeight);
        UIView  * pTitleView = [[UIView alloc] initWithFrame:frame];
        pTitleView.backgroundColor = [UIColor clearColor];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(-10, 0, SCREEN_WIDTH, iItemHeight)];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.font = [UIFont boldSystemFontOfSize:18];
        titleLabel.textColor = [UIColor colorWithString:COLOR_FFFFFF];
        titleLabel.text = strTitle;
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [pTitleView addSubview:titleLabel];
        
        if(iTitleIndex == TabBarItemTypeSupervise){
            UIImage *img = [UIImage imageNamed:@"my_location_icon"];
            
            UIImageView *iconView = [[UIImageView alloc] initWithFrame:CGRectMake(-3, (iItemHeight - img.size.height) / 2, img.size.width, img.size.height)];
            [iconView setImage:img];
            [iconView setUserInteractionEnabled:YES];
            [pTitleView addSubview:iconView];
            [iconView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(locationTap:)]];
            
            pItem.title = nil;
            pItem.titleView = pTitleView;
        }
    }
}

- (void)locationTap:(UIGestureRecognizer *)recognizer
{
    MainViewController *main = [AppUtils getmainViewController];
    SuperviseViewController *controller = main.viewControllers[TabBarItemTypeSupervise];
    [controller locationMyPosition];
}

@end
